package com.caituo.idouyou.user.test;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;

/**
 * @author fisher
 * @description 用户视图层
 * @date 2011-12-12
 */

public class UserAction extends BaseAction<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// 用户对象
	private User user;
	
	// 返回结果记录集
	private List<User> result;

	@Override
	public User getModel() {
		return user;
	}

	@Override
	public void prepare() throws Exception {
		// 在调用Action方法之前先初始化user对象
		user = new User();
	}

	/**
	 * @description 查询用户数据
	 * @return
	 */

	public String view() {
		// 调用后台方法,得到返回数据成List<User>，后台方法我就没写了,我直接new
		result = new ArrayList<User>();
		// 这里我来响应不同类型了,注意:
		/**
		 * jsp视图
		 * 
		 */
		this.setTarget("/page/user.jsp");
		return SUCCESS;
		/**
		 * JSON renderJson(result); return NONE;
		 */

		/**
		 * redirect this.setTarget("/page/user.jsp"); return "redirect";
		 */

		/**
		 * redirectAction this.setTarget("userAction"); return "redirectAction"
		 */

	}

	/**
	 * @description 更新用户数据
	 * @return
	 */
	public String update() {
		// 当前台传入参数后，经过modeldriven拦截器封装后,会赋给我们user变量中，调用后台方法
		// service.update(user);
		this.setTarget("/page/user.jsp");
		return SUCCESS;
	}

  /**
   * 
   * @description 处理流文件，直接在网页上打开图片
   * @return
 * @throws Exception 
   */
	public String preview () throws IOException{
		try {
			//我们得到图片二进制流,这里我是NEW出来的
			byte[] imgBytes =new byte[]{};
			InputStream in = new ByteInputStream(imgBytes, imgBytes.length);
			HttpServletResponse response = this.getResponse();
			OutputStream os = response.getOutputStream();
			int len = 0;
			while ((len = in.read(imgBytes)) != -1) {
				os.write(imgBytes, 0, len);
			}
			os.close();
			in.close();
			response.setContentType("image/jpeg");
			this.setTarget("/page/userPreview.jsp");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	
	public List<User> getResult() {
		return result;
	}

	public void setResult(List<User> result) {
		this.result = result;
	}

}
